package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 频道-模糊分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeSearch(ChannelDto dto) {
        // 1 设置分页参数
        Page<WmChannel> pageSearch = new Page<>(dto.getPage(), dto.getSize());
        // 2 封装查询条件
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(WmChannel::getName, dto.getName())//默认传空字串，不需要判空
                .orderByDesc(WmChannel::getCreatedTime);
//                .eq(WmChannel::getStatus,true)
//                .eq(WmChannel::getStatus,false);// TODO:账号状态分组查询
        // 3 分页查询
        Page<WmChannel> pageInfo = page(pageSearch, wrapper);
        // 4 封装返回对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageInfo.getTotal());
        result.setData(pageInfo.getRecords());
        return result;

    }

    /**
     * 新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult addChannel(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName, wmChannel.getName());
        WmChannel one = getOne(wrapper);
        if (one == null) {
            if (wmChannel.getCreatedTime() == null) {
                wmChannel.setCreatedTime(new Date());
            }
            save(wmChannel);
        } else {
            return ResponseResult.error(AppHttpCodeEnum.DATA_EXIST);
        }
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     * TODO:如果频道被引用则不能禁用
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
//        WmChannel one = getById(wmChannel.getId());
//        if (one.getStatus() == true) {
//            wmChannel.setStatus(one.getStatus());
//        }
        updateById(wmChannel);
        return ResponseResult.okResult();
    }

    /**
     * 删除禁用频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        WmChannel wmChannel = getById(id);
            if (wmChannel.getStatus() == false) {
                removeById(id);
                return ResponseResult.okResult();
            }
        return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_DELETE);
    }
}
